#include "registerwindow.h"
#include "ui_registerwindow.h"

// 构造函数
RegisterWindow::RegisterWindow(QWidget *parent)
    : QDialog(parent), ui(new Ui::RegisterWindow)
{
    ui->setupUi(this);

    // 连接信号槽
    connect(ui->registerButton, &QPushButton::clicked, this, &RegisterWindow::onRegisterClicked);
    connect(ui->cancelButton, &QPushButton::clicked, this, &RegisterWindow::onCancelClicked);

    // 设置窗口属性
    setWindowTitle("用户注册");
    setModal(true); // 设置为模态对话框

    // 设置输入框回车事件
    connect(ui->confirmPasswordLineEdit, &QLineEdit::returnPressed, this, &RegisterWindow::onRegisterClicked);
}

// 析构函数
RegisterWindow::~RegisterWindow()
{
    delete ui;
}

// 注册按钮点击事件
void RegisterWindow::onRegisterClicked()
{
    if (!validateRegistration())
    {
        return;
    }

    if (registerUser())
    {
        showSuccess("注册成功！");
        ui->usernameLineEdit->clear();
        ui->passwordLineEdit->clear();
        ui->confirmPasswordLineEdit->clear();
        emit registrationSuccessful();
        this->accept(); // 关闭对话框
    }
}

// 取消按钮点击事件
void RegisterWindow::onCancelClicked()
{
    this->reject(); // 取消并关闭对话框
}

// 验证注册信息
bool RegisterWindow::validateRegistration()
{
    QString username = ui->usernameLineEdit->text().trimmed();
    QString password = ui->passwordLineEdit->text();
    QString confirmPassword = ui->confirmPasswordLineEdit->text();

    // 检查用户名
    if (username.isEmpty())
    {
        showError("请输入用户名！");
        ui->usernameLineEdit->setFocus();
        return false;
    }

    if (username.length() < 3)
    {
        showError("用户名长度不能少于3个字符！");
        ui->usernameLineEdit->setFocus();
        return false;
    }

    // 检查用户名是否已存在
    if (DatabaseManager::getInstance().isUsernameExists(username))
    {
        showError("该用户名已存在，请选择其他用户名！");
        ui->usernameLineEdit->setFocus();
        return false;
    }

    // 检查密码
    if (password.isEmpty())
    {
        showError("请输入密码！");
        ui->passwordLineEdit->setFocus();
        return false;
    }

    if (password.length() < 6)
    {
        showError("密码长度不能少于6个字符！");
        ui->passwordLineEdit->setFocus();
        return false;
    }

    // 检查密码确认
    if (confirmPassword.isEmpty())
    {
        showError("请确认密码！");
        ui->confirmPasswordLineEdit->setFocus();
        return false;
    }

    if (password != confirmPassword)
    {
        showError("两次输入的密码不一致！");
        ui->confirmPasswordLineEdit->setFocus();
        return false;
    }

    return true;
}

// 执行用户注册
bool RegisterWindow::registerUser()
{
    QString username = ui->usernameLineEdit->text().trimmed();
    QString password = ui->passwordLineEdit->text();

    if (DatabaseManager::getInstance().registerUser(username, password))
    {
        return true;
    }
    else
    {
        showError("注册失败，请重试！");
        return false;
    }
}

// 显示错误信息
void RegisterWindow::showError(const QString &message)
{
    QMessageBox::warning(this, "注册错误", message);
}

// 显示成功信息
void RegisterWindow::showSuccess(const QString &message)
{
    QMessageBox::information(this, "提示", message);
}